#include <func.h>

int main(int argc,char *argv[])
{
    ARGS_CHECK(argc,3);
    int socketfd;
    //初始化一个socket，有一个缓冲区
    socketfd=socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(socketfd,-1,"socket");
    printf("socketfd=%d\n",socketfd);
    struct sockaddr_in ser;
    bzero(&ser,sizeof(ser));
    ser.sin_family=AF_INET;
    ser.sin_addr.s_addr=inet_addr(argv[1]);
    ser.sin_port=htons(atoi(argv[2]));
    int ret;
    ret=bind(socketfd,(struct sockaddr*)&ser,sizeof(struct sockaddr));
    ERROR_CHECK(ret,-1,"bind");
    listen(socketfd,10);
    int newfd;
    struct sockaddr_in cli;
    bzero(&cli,sizeof(cli));
    socklen_t len=sizeof(cli);
    newfd=accept(socketfd,(struct sockaddr*)&cli,&len);
    ERROR_CHECK(newfd,-1,"accept");
    printf("client ip=%s,client port=%d\n",
           inet_ntoa(cli.sin_addr),ntohs(cli.sin_port));
    char buf[128]={0};
    recv(newfd,buf,sizeof(buf),0);
    printf("I am server!,gets:%s\n",buf);
    return 0;
}

